package com.lzh.farmersshop.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzh.farmersshop.util.Result;
import lombok.extern.slf4j.Slf4j;
import com.lzh.farmersshop.entity.Admininfo;
import com.lzh.farmersshop.service.AdmininfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * (Admininfo)表控制层
 *
 * @author makejava
 * @since 2024-01-04 15:41:50
 */
@Slf4j
@RestController
@RequestMapping("admin/admininfo")
@Transactional(rollbackFor = Exception.class)
public class AdmininfoController {
    /**
     * 服务对象
     */
    @Resource
    private AdmininfoService admininfoService;

    /**
     * 分页查询所有数据
     *
     * @param admininfo 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public Result selectAll(Admininfo admininfo,Integer page,Integer limit) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        LambdaQueryWrapper<Admininfo> q = new LambdaQueryWrapper<>();
        q.like(StringUtils.hasLength(admininfo.getPhone()), Admininfo::getPhone, admininfo.getPhone());
        q.like(StringUtils.hasLength(admininfo.getUsername()), Admininfo::getUsername, admininfo.getUsername());
        HashMap<String, Object> result = new HashMap<>();
        result.put("count", admininfoService.list(q).size());
        //分页
        q.last("limit " + (page - 1) * limit + "," + limit);
        result.put("data", admininfoService.list(q));
        return Result.success(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        return Result.success(admininfoService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param admininfo 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public Result insert(Admininfo admininfo) {
        //判断admininfo是否为空，为空则返回不能为空
        if (admininfo == null) {
            return Result.failure("数据不能为空");
        }

        //判断该手机号是否已经存在
        if (admininfoService.getOne(new QueryWrapper<Admininfo>().eq("phone", admininfo.getPhone()))!= null) {
            return Result.failure("该手机号已经存在");
        }

        admininfo.setIsdelete(1);
        if (admininfoService.save(admininfo)) {
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    /**
     * 修改数据
     *
     * @param admininfo 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Result update(Admininfo admininfo, HttpServletRequest request) {
        //判断admininfo是否为空，为空则返回不能为空
        if (admininfo == null) {
            return Result.failure("数据不能为空");
        }

        Admininfo phone = admininfoService.getOne(new QueryWrapper<Admininfo>().eq("phone", admininfo.getPhone()));
        if (phone!= null &&!phone.getId().equals(admininfo.getId())) {
            return Result.failure("该手机号已经存在");
        }

        if (admininfoService.updateById(admininfo)) {
            // 获取Session对象
            HttpSession session = request.getSession();
            //获取用户信息
            Admininfo admin = (Admininfo) session.getAttribute("admininfo");
            //判断修改人是否是自己
            if (admin.getId().equals(admininfo.getId())) {
                //重新设置session中的admin信息
                session.setAttribute("admininfo", admininfo);
            }
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @PostMapping("delete")
    public Result delete(Integer id) {
        //判断id是否为空
        if (id == null) {
            return Result.failure("id不能为空");
        }

        Admininfo admininfo = new Admininfo();
        admininfo.setIsdelete(2);
        admininfo.setId(id);

        if (admininfoService.removeById(admininfo)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }

    /**
     * 登录
     * @param phone
     * @param password
     * @param request
     * @return
     */
    @PostMapping("login")
    public Result login(String phone, String password, HttpServletRequest request) {
        //判断参数是否为空
        if(phone==null||password==null){
            return Result.failure("手机号和密码不能为空");
        }

        //根据账号查找
        QueryWrapper<Admininfo> admin = new QueryWrapper<>();
        admin.eq("phone",phone);
        Admininfo admininfo = admininfoService.getOne(admin);
        if(admininfo==null){
            return Result.failure("账号不存在");
        }

        if(!admininfo.getPassword().equals(password)){
            return Result.failure("密码错误");
        }

        // 获取Session对象
        HttpSession session = request.getSession();

        // 将用户信息存储到Session中
        session.setAttribute("admininfo", admininfo);
        return Result.success(admininfo.getUsername());
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @GetMapping("logout")
    public Result logout(HttpServletRequest request) {
        // 获取Session对象
        HttpSession session = request.getSession();

        // 将用户信息从Session中删除
        session.removeAttribute("admininfo");
        return Result.success("退出成功");
    }
}

